#!/bin/bash

. $(dirname $0)/functions

BASE=$BASEDIR/
BUILD=$BUILDDIR/

if [ -n "$MSYSTEM" ]; then
    # Avoid Windows drive letter issues in the path of DEF:vname=rrdfile under MSYS or MSYS2
    RRD=rpn2.rrd
else
    RRD=${BUILD}rpn2.rrd
fi

$RRDTOOL create $RRD --step 7200 --start 1167487000 DS:speed:DCOUNTER:14000:U:U RRA:AVERAGE:0.5:1:30
report "create"

# Sunday 2006-12-31T23:50:00 = 1167605400
# is a reat test for the wrap detector as they ALL should wrap now

$RRDTOOL update $RRD 1167487200:0 1167494400:720 1167501600:1440 1167508800:2160 1167516000:2880 1167523200:3600 1167530400:4320 1167537600:5040 1167544800:5760 1167552000:6480 1167559200:7200 1167566400:7920 1167573600:8640 1167580800:9360 1167588000:10080 1167595200:10800 1167602400:11520 1167609600:12240 1167616800:12960

report "update"
LC_ALL= LC_TIME=C TZ=Europe/Zurich $RRDTOOL xport \
      --json \
      --start  1167487200 --end 1167616800       \
      DEF:myspeed=$RRD:speed:AVERAGE              \
      CDEF:rday=myspeed,POP,NEWDAY \
      CDEF:rweek=myspeed,POP,NEWWEEK \
      CDEF:rmonth=myspeed,POP,NEWMONTH \
      CDEF:ryear=myspeed,POP,NEWYEAR \
      CDEF:day=myspeed,STEPWIDTH,*,NEWDAY,0,PREV,IF,ADDNAN             \
      CDEF:week=myspeed,STEPWIDTH,*,NEWWEEK,0,PREV,IF,ADDNAN             \
      CDEF:month=myspeed,STEPWIDTH,*,NEWMONTH,0,PREV,IF,ADDNAN             \
      CDEF:year=myspeed,STEPWIDTH,*,NEWYEAR,0,PREV,IF,ADDNAN             \
      CDEF:avg=myspeed,POP,1,1,1,17,4,AVG \
      CDEF:smin=myspeed,POP,3,4,5,2.2,4,SMIN \
      CDEF:smax=myspeed,POP,3,4,5,2.2,4,SMAX \
      CDEF:med1=myspeed,POP,3,4,5,2.2,4,MEDIAN \
      CDEF:med2=myspeed,POP,3,4,5,2.2,11,5,MEDIAN \
      CDEF:stdev=myspeed,POP,3,4,5,2.2,4,STDEV \
      CDEF:p9=myspeed,POP,0.5,1,1,1,1,1,1,1,5,10,100,10,PERCENT \
      CDEF:pow=myspeed,2,POW \
      XPORT:myspeed:myspeed \
      XPORT:day:day       XPORT:rday:rday \
      XPORT:week:week  XPORT:rweek:rweek \
      XPORT:month:month       XPORT:rmonth:rmonth \
      XPORT:year:year      XPORT:ryear:ryear \
      XPORT:avg:avg \
      XPORT:smin:smin \
      XPORT:smax:smax \
      XPORT:med1:med1 \
      XPORT:med2:med2 \
      XPORT:p9:p9 \
      XPORT:pow:pow \
      XPORT:stdev:stdev |\

 $DIFF9 - $BASEDIR/rpn2.output
report "xport"
